package scalab.core

import component.{SinkComponent, Component}
import gates.{Input, Output}

trait Provider extends Buildable{

	def output: Output
	val component: Component
	//public interface
	final def ->(in: Input): SinkComponent = {
		//Report.note(sink)
		in flowsOut output //override flowsOut in In?
    in.component.asInstanceOf[SinkComponent]
  }

	final def ->(target: SinkComponent): SinkComponent = {
		if(!target.inputs.isEmpty && target.inputs.tail.isEmpty)
			->(target.inputs.head)
		else if(target.inputs.isEmpty)
			this.component.fail("Cannot provision " + target.name + " since the latter component does not have any input!")
		else
			this.component.fail("Cannot decide implicit input for component " + target.name + " since the latter component has more than one input!")
		target
	}

	final def ->(recipient: Recipient): SinkComponent = recipient match{
		case in: Input => ->(in)
		case cc: SinkComponent => ->(cc)
	}

	final def >>(recipients: Iterable[Recipient]): Unit = recipients foreach {r => output -> r}
	final def >>(recipients: Recipient*): Unit = >>(recipients)

}
